<?php
namespace Versal\SecurityBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class LoginController extends Controller
{
    public function loginAction(Request $request)
    {		
	    $session = $request->getSession();
        $error = "";
        // get the login error if there is one
        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
            $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
        } else {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);            
            $session->remove(SecurityContext::AUTHENTICATION_ERROR);
        }
        
        if($error instanceof \Symfony\Component\Security\Core\Exception\BadCredentialsException){
            $error = "Nombre de usuario o contraseña incorrectos";
        }
        
        return $this->render('VersalSecurityBundle:Default:loginForm.html.php',
            array(
                // last username entered by the user
                'last_username' => $session->get(SecurityContext::LAST_USERNAME),
                'error'         => $error             
            )
        );   
        
        
        
    }
    
    public function successAction(Request $request){       
         /** REDIRECCIONA A LA PAGINA PRINCIPAL**/
         $url = $this->get('router')->generate('versal_admin_homepage',array(), true);
         return $this->redirect($url);
    }
    
  
}
